package com.haofengsoft.scce.ui.consultation;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.InvestmentTipAdapter;
import com.haofengsoft.scce.adapter.information.InvestmentTipModel;
import com.haofengsoft.scce.db.entity.consultation.InvestmentTip;
import com.haofengsoft.scce.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class InvestmentTipsFragment extends BaseFragment {

    private PullToRefreshListView investmentTopsPtrl;
    private InvestmentTipAdapter tipAdapter;
    private Context context;

    private int pageIndex = 0;


    public static InvestmentTipsFragment newInstance(String name) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        InvestmentTipsFragment fragment = new InvestmentTipsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.consultation_stamp_investment_tips, container, false);
        view.setOnTouchListener(new DontSpillOnTouchListener());
        context = getActivity();
        view.setOnTouchListener(new DontSpillOnTouchListener());
        investmentTopsPtrl = (PullToRefreshListView) view
                .findViewById(R.id.consultation_stamp_investment_tips_ptrl);
        investmentTopsPtrl.setMode(PullToRefreshBase.Mode.DISABLED);

        ImageView nodataIv = (ImageView) view.findViewById(R.id.investment_tips_no_data_iv);
        nodataIv.setOnClickListener(new DataLoadOnClickListener());
        investmentTopsPtrl.setEmptyView(nodataIv);
        investmentTopsPtrl.setOnFocusChangeListener(null);
        investmentTopsPtrl.setOnHierarchyChangeListener(null);
        investmentTopsPtrl.setOnRefreshListener(new LoadPullData());
        initVolleyData();
        return view;
    }

    class DataLoadOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            initVolleyData();
        }
    }

    class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
        // 下拉(手势下拉)
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            pageIndex = 0;
            tipAdapter = null;
            initVolleyData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            initVolleyData();
        }
    }

    private void initVolleyData() {
        List<InvestmentTip> tipList = new ArrayList<InvestmentTip>();
        InvestmentTip tip1 = new InvestmentTip();
        tip1.setTitle("守得云开见月明 市场重回正轨");
        tip1.setContent("对于6月中旬之后发生在A股市场的罕见股灾，在管理层的积极推动下终于在7月8日结束了集体性的暴跌，并出现快速的反弹....");
        tip1.setDateTime("07-25 21:56");
        tipList.add(tip1);
        tip1 = new InvestmentTip();
        tip1.setTitle("高位震荡 如何寻找投资新高地");
        tip1.setContent("受益于打新资金和降息的双重利好，本周大盘初始连续收阳，反弹到4400点这一前期小平台后，压力明显增大...");
        tip1.setDateTime("07-25 21:56");
        tipList.add(tip1);
        tip1 = new InvestmentTip();
        tip1.setTitle("走出去+并购重组 军工主题再升温");
        tip1.setContent("我国与俄罗斯在莫斯科共同发表《中华人民共和国和俄罗斯联邦关于深化全面战略协作伙伴关系、倡导合作共赢的联合声明》，并签署32项合作项目，涉及金额达250亿美元，其中军工航天领域占据重要份额。走出去叠加并购重组...");
        tip1.setDateTime("07-25 21:56");
        tipList.add(tip1);

        tip1 = new InvestmentTip();
        tip1.setTitle("风险与机会并存，牛市中期格局不变");
        tip1.setContent("任何变革都是螺旋式上升，在支持宏观经济的国家战略任务未完成之前，管理层不会出台真正的重大利空，来改目前的慢牛格局...");
        tip1.setDateTime("07-25 21:56");
        tipList.add(tip1);

        tip1 = new InvestmentTip();
        tip1.setTitle("杭州要建跨境电商顶级智库");
        tip1.setContent("日前召开的中国(杭州)跨境电商综试区建设推进大会，吹响了打造全球最优跨境电商生态圈的“集结号”，杭州跨境电商综试区《实施方案》提及的“三大中心”、“六个体系”以及“两个平台”...");
        tip1.setDateTime("07-25 21:56");
        tipList.add(tip1);

        tip1 = new InvestmentTip();
        tip1.setTitle("农垦改革新政出炉倒计时");
        tip1.setContent("据相关农垦改革负责人透露，农垦改革方案已经审议通过，此次改革将成为近年来规模最大，最全面的一次。随着改革方案的出台，相关改革企业也都将在这近几天公布具体改革进度...");
        tip1.setDateTime("07-25 21:56");
        tipList.add(tip1);
        List<InvestmentTipModel> modelList = new ArrayList<InvestmentTipModel>();
        for (InvestmentTip tip : tipList) {
            InvestmentTipModel model = new InvestmentTipModel();
            model.setContent(tip.getContent());
            model.setDateTime(tip.getDateTime());
            model.setTitle(tip.getTitle());
            modelList.add(model);
        }

        refreshList(modelList);

        // Map<String, Object> mapParam = new HashMap<String, Object>();
        // mapParam.put("PageIndex", pageIndex);
        // mapParam.put("PageSize", 10);
        // mapParam.put("TypeID", 1);
        //
        // VolleyManage.getInstance().methodPost(getActivity(), null,
        // ConstantConfig.TYPE_NEWS_URL, mapParam,
        // new GuestColumnVolleyReal());

    }

    private void refreshList(List<InvestmentTipModel> modelList) {
        if (null != modelList) {
            if (null == tipAdapter) {
                tipAdapter = new InvestmentTipAdapter(context, modelList);
                investmentTopsPtrl.setAdapter(tipAdapter);
            } else {
                tipAdapter.addInvestTipList(modelList);
            }
            pageIndex = pageIndex + 1;
            tipAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getActivity(), "没有信息了", Toast.LENGTH_SHORT).show();
        }
        investmentTopsPtrl.onRefreshComplete();
    }

    @Override
    protected void lazyLoad() {

    }
}
